package com.servlet;

import com.bean.Building;
import com.bean.Room;
import com.google.gson.Gson;
import com.service.impl.RoomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/RoomServlet/*")
public class RoomServlet extends HttpServlet {

    RoomServiceImpl roomService = new RoomServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");//命令浏览器用什么编码打开文件

        String pathInfo = request.getPathInfo().substring(1);
        if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if("deleteById".equals(pathInfo)){
            deleteById(request,response);
        }else if("removeAll".equals(pathInfo)){
            removeAll(request,response);
        }else if("listAllByPagination".equals(pathInfo)){
            listAllByPagination(request,response);
        }else if("updateById".equals(pathInfo)){
            updateById(request,response);
        }else if("listAllByPaginationSearch".equals(pathInfo)){
            listAllByPaginationSearch(request,response);
        }
    }

    private void listAllByPaginationSearch(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));
        String filter  = request.getParameter("filter");
        Gson gson = new Gson();
        Map<String,String> map1 = gson.fromJson(filter,Map.class);

        try {
            List<Room> roomList = roomService.selectAllPaginationSearch(offset,limit,map1);

            int total = roomService.countSearch(map1);
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",roomList);


            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateById(HttpServletRequest request, HttpServletResponse response) {
        Integer roomId = Integer.valueOf(request.getParameter("roomId"));
        String roomNumber = request.getParameter("roomNumber");
        Integer unitId = Integer.valueOf(request.getParameter("unitId"));
        Integer buildId = Integer.valueOf(request.getParameter("buildId"));
        String roomType = request.getParameter("roomType");
        String roomDirection = request.getParameter("roomDirection");
        String roomStatus = request.getParameter("roomStatus");
        Integer floorCode = Integer.valueOf(request.getParameter("floorCode"));
        Integer roomArea = Integer.valueOf(request.getParameter("roomArea"));
        String roomDesc = request.getParameter("roomDesc");

        Room room = new Room(roomId,roomNumber,unitId,buildId,roomType,roomDirection,roomStatus,floorCode,roomArea,roomDesc);

        try {
            boolean b = roomService.update(room);
            System.out.println(b);
            if(b){
                response.getWriter().print("success");
            }else{
                response.getWriter().print("");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void listAllByPagination(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));

        try {
            List<Room> buildingList = roomService.selectAllPagination(offset,limit);
            int total = roomService.count();
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",buildingList);

            Gson gson = new Gson();
            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void removeAll(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        System.out.println(ids);
        String[] idsArr = ids.split(",");
        try {
            boolean b = roomService.removeAll(idsArr);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) {
        Integer roomId = Integer.valueOf(request.getParameter("roomId"));
        try {
            boolean b = roomService.deleteById(roomId);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAll(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Room> buildingList = roomService.selectAll();
            Gson gson = new Gson();
            String s = gson.toJson(buildingList);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
